
package fr.mehfile.api.parser;

import java.net.MalformedURLException;
import java.net.URL;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;

public class XmlAgentParserHandler extends DefaultHandler {

    // attribute In
    private boolean isIn_base = false;
    private boolean isIn_Programs = false;
    private boolean isIn_Schedules = false;
    private boolean isIn_News = false;
    private boolean isIn_Update_Ios = false;
    private boolean isIn_Update_Android = false;

    // TAG
    private final String TAG_BASE = "base";
    private final String TAG_VERSION = "version";
    private final String TAG_SERVER_URL = "server_url";
    private final String TAG_PROGRAMS = "programs";
    private final String TAG_PROGRAM = "program";
    private final String TAG_DATE = "date";
    private final String TAG_TITLE = "title";
    private final String TAG_DESCRIPTION = "description";
    private final String TAG_SCHEDULES = "schedules";
    private final String TAG_SCHEDULE = "schedule";
    private final String TAG_START_TIME = "start_time";
    private final String TAG_END_TIME = "end_time";
    private final String TAG_NEWS = "news";
    private final String TAG_CURRENT_NEWS = "current_news";
    private final String TAG_CONTENT = "content";
    private final String TAG_IMAGE = "image";
    private final String TAG_ANDROID = "android";
    private final String TAG_URL = "url";
    private final String TAG_IOS = "ios";

    // attribute
    private UpdateData currentUpdateData;
    private StringBuilder stringbuilder;
    private Programs programs;
    private News news;
    private Program currentProgram;
    private CurrentNews currentNews;
    private Schedule currentSchedule;
    private Schedules schedules;

    public XmlAgentParserHandler(Context context) {
        currentUpdateData = new UpdateData();
        stringbuilder = new StringBuilder();

    }

    public UpdateData getUpdateData() {
        return currentUpdateData;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {
        if (TAG_BASE.equals(localName)) {
            isIn_base = true;
        } else if (TAG_VERSION.equals(localName)) {
            stringbuilder.setLength(0);
        } else if (TAG_SERVER_URL.equals(localName)) {
            stringbuilder.setLength(0);
        } else if (TAG_PROGRAMS.equals(localName)) {
            isIn_Programs = true;
            programs = new Programs();
        } else if (TAG_PROGRAM.equals(localName)) {
            currentProgram = new Program();
        } else if (TAG_DATE.equals(localName)) {
            stringbuilder.setLength(0);
        } else if (TAG_TITLE.equals(localName)) {
            stringbuilder.setLength(0);
        } else if (TAG_DESCRIPTION.equals(localName)) {
            stringbuilder.setLength(0);
        } else if (TAG_SCHEDULES.equals(localName)) {
            isIn_Schedules = true;
            schedules = new Schedules();
        } else if (TAG_SCHEDULE.equals(localName)) {
            currentSchedule = new Schedule();
        } else if (TAG_START_TIME.equals(localName)) {
            stringbuilder.setLength(0);
        } else if (TAG_END_TIME.equals(localName)) {
            stringbuilder.setLength(0);
        } else if (TAG_NEWS.equals(localName)) {
            isIn_News = true;
            news = new News();
        } else if (TAG_CURRENT_NEWS.equals(localName)) {
            currentNews = new CurrentNews();
        } else if (TAG_CONTENT.equals(localName)) {
            stringbuilder.setLength(0);
        } else if (TAG_IMAGE.equals(localName)) {
            stringbuilder.setLength(0);
        } else if (TAG_IOS.equals(localName)) {
            isIn_Update_Ios = true;
        } else if (TAG_URL.equals(localName)) {
            stringbuilder.setLength(0);
        } else if (TAG_ANDROID.equals(localName)) {
            isIn_Update_Android = true;
        }
        if (isIn_base == false) {
            throw new SAXException("balise <base> malformed or absent");
        }

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (TAG_BASE.equals(localName)) {
            isIn_base = false;
        } else if (TAG_VERSION.equals(localName)) {
            try {
                int version = Integer.parseInt(stringbuilder.toString().trim());

               if (isIn_base && isIn_Update_Ios == false)
                    currentUpdateData.setXmlversion(version);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (TAG_SERVER_URL.equals(localName)) {
            try {
                URL serverUrl = new URL(stringbuilder.toString().trim());
                currentUpdateData.setServerUrl(serverUrl);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        } else if (TAG_PROGRAMS.equals(localName)) {
            isIn_Programs = false;
            currentUpdateData.setPrograms(programs);
        } else if (TAG_PROGRAM.equals(localName)) {
            currentProgram.setSchedules(schedules);
            programs.getProgram().add(currentProgram);
           
        } else if (TAG_NEWS.equals(localName)) {
            isIn_News = false;
            currentUpdateData.setNews(news);
        } else if (TAG_CURRENT_NEWS.equals(localName)) {
            news.getCurrentNews().add(currentNews);
        } else if (TAG_DATE.equals(localName)) {
            if (isIn_News) {
                currentNews.setDate(stringbuilder.toString().trim());
            } else if (isIn_Programs) {
            	currentProgram.setDate(stringbuilder.toString().trim());
            }
            
        } else if (TAG_TITLE.equals(localName)) {
            if (isIn_Programs && !isIn_Schedules) {
                currentProgram.setTitle(stringbuilder.toString().trim());
            } else if (isIn_News) {
                currentNews.setTitle(stringbuilder.toString().trim());
            } else if (isIn_Schedules) {
                currentSchedule.setTitle(stringbuilder.toString().trim());
            }
        } else if (TAG_DESCRIPTION.equals(localName)) {
            currentProgram.setDescription(stringbuilder.toString().trim());
        } else if (TAG_SCHEDULES.equals(localName)) {
            isIn_Schedules = false;
            
        } else if (TAG_SCHEDULE.equals(localName)) {
            schedules.getSchedule().add(currentSchedule);
        } else if (TAG_START_TIME.equals(localName)) {
            currentSchedule.setStart_time(stringbuilder.toString().trim());
        } else if (TAG_END_TIME.equals(localName)) {
            currentSchedule.setEnd_time(stringbuilder.toString().trim());
        } else if (TAG_CONTENT.equals(localName)) {
            currentNews.setContent(stringbuilder.toString().trim());
        } else if (TAG_IMAGE.equals(localName)) {
            try {
                URL url = new URL(stringbuilder.toString().trim());
               currentNews.setImage(url);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        } else if (TAG_ANDROID.equals(localName)) {
            isIn_Update_Android = false;
        } else if (TAG_ANDROID.equals(localName)) {
            isIn_Update_Ios = false;
        } 
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        stringbuilder.append(new String(ch, start, length));
    }
}
